/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servicios;

import beans.AdmUsuario;
import beans.AdminEmail;
import beans.AdminNoticia;
import beans.Noticia;
import beans.Usuario;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.jasper.tagplugins.jstl.core.Redirect;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Regina
 */
public class ServletNoticias extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String strTipo = request.getParameter("tipo");
        if(strTipo != null)
        {
            int tipo = Integer.parseInt(strTipo);
            switch (tipo) {
                case 1:
                    guardarNoticia(request, response);
                    break;
                case 2:
                    enviarNoticias(request,response);
                    break;
            }
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void guardarNoticia(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Noticia noticia=getNoticia(request);
             
        if (new AdminNoticia().registrarNoticia(noticia)) {
            //RequestDispatcher rd=request.getRequestDispatcher("recuperarUsuario.jsp");
            //rd.forward(request, response);         
                        
        } else {
            //response.sendRedirect("registro.jsp");
        }
    }
    
    private void enviarNoticias(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String usuarios = request.getParameter("usuarios");
        String categoria = request.getParameter("categoria");
        
        AdmUsuario admUsuario = new AdmUsuario();
        ArrayList<Usuario> listaUsuarios = admUsuario.getUsuariosPorIds(usuarios);
        StringBuilder emails = new StringBuilder();
        for (Usuario usuario : listaUsuarios) {
            emails.append(usuario.getCorreo());
            emails.append(";");
        }
        
        AdminNoticia admNoticia = new AdminNoticia();
        ArrayList<Noticia> noticias = admNoticia.getNoticiasPorCategoria(categoria);
        AdminEmail email = new AdminEmail();
        for (Noticia noticia : noticias) {
            email.enviarCorreoNoticias(emails.toString(), noticia.getTitulo(), noticia.getNoticia());
        }
    }

    private Noticia getNoticia(HttpServletRequest request) {
        Noticia noticia = new Noticia(request.getParameter("titulo"), request.getParameter("categoria"), request.getParameter("noticia"));
        return noticia;
    }
    
}